package com.ruoyi.business.controller.app;

import com.ruoyi.business.service.IWsdLeavesService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.LeaveSummaryDTO;
import com.ruoyi.common.core.domain.dto.submitLeaveDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdLeaves;
import com.ruoyi.common.core.domain.vo.LeaveSummaryVO;
import com.ruoyi.common.core.domain.vo.RefundAvailabilityVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * 请假Controller  小程序
 * 
 * @author ruoyi
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/wx_leaves")
public class WxAppWsdLeavesController extends BaseController {

    @Autowired
    private IWsdLeavesService wsdLeavesService;

    /**
     * 请假信息列表
     */
    @GetMapping(value = "/LeaveSummary")
    public TableDataInfo LeaveSummary(LeaveSummaryDTO leaveSummaryDTO) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        leaveSummaryDTO.setSchoolId(schoolId.toString());
        startPage();
        List<LeaveSummaryVO> leaveSummaryVOS = wsdLeavesService.LeaveSummary(leaveSummaryDTO);
        return getDataTable(leaveSummaryVOS);
    }

    /**
     * 提交请假请求
     */
    @Log(title = "家长提交请假请求", businessType = BusinessType.INSERT)
    @PostMapping("/submitLeave")
    @RepeatSubmit
    public AjaxResult submitLeave(@RequestBody submitLeaveDTO submitLeaveDTO) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        Long userId = user.getUserId();
        submitLeaveDTO.setSchoolId(schoolId);
        submitLeaveDTO.setUserId(userId);
        //查询当前学生在今天有没有请假
        int selectStudentsWsdLeave = wsdLeavesService.selectStudentsWsdLeave(submitLeaveDTO.getStudentId(),submitLeaveDTO.getStartTime(),submitLeaveDTO.getEndTime());
        if (selectStudentsWsdLeave != 0){
            return AjaxResult.error("今天已经请假了，不能重复请假");
        }
        return toAjax(wsdLeavesService.submitLeave(submitLeaveDTO));
    }

    /**
     * 获取学生请假详细信息
     */
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(LeaveSummaryDTO leaveSummaryDTO) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        leaveSummaryDTO.setSchoolId(schoolId.toString());
        LeaveSummaryVO leaveSummaryVO = wsdLeavesService.getInfo(leaveSummaryDTO);
        return AjaxResult.success(leaveSummaryVO);
    }

    /**
     * 获取学生请假早中晚餐
     */
    @GetMapping(value = "/getInfoStudent")
    public AjaxResult getInfoStudent(LeaveSummaryDTO leaveSummaryDTO) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        leaveSummaryDTO.setSchoolId(schoolId.toString());
        List<RefundAvailabilityVO> list = wsdLeavesService.getInfoStudent(leaveSummaryDTO);
        return AjaxResult.success(list);
    }

    /**
     * 撤回请假申请
     */
    @Log(title = "撤回请假申请", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(wsdLeavesService.deleteWsdLeavesById(id));
    }

    /**
     * 查询确定可退款的餐
     */
    @GetMapping("/determineRefundableMeals")
    @Anonymous
    public AjaxResult determineRefundableMeals(WsdLeaves wsdLeaves) {
        List<RefundAvailabilityVO> refundableDays = new ArrayList<>();

        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 转换请假的开始和结束时间为日期
        LocalDate startDate = LocalDateTime.ofInstant(wsdLeaves.getStartTime().toInstant(), ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = LocalDateTime.ofInstant(wsdLeaves.getEndTime().toInstant(), ZoneId.systemDefault()).toLocalDate();

        // 遍历请假日期范围，为每一天检查早、中、晚餐的退款情况
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            boolean breakfast = true;
            boolean lunch = true;
            boolean dinner = true;

            // 检查请假日期是否包括今天，如果包括，则今天所有餐次均不可退款
            if (date.isEqual(today)) {
                breakfast = false;
                lunch = false;
                dinner = false;
            }

            // 检查请求时间是否晚上6点后，并且请假的开始日期是明天
            if (LocalDateTime.now().toLocalTime().isAfter(LocalTime.of(18, 0)) && date.isEqual(today.plusDays(1))) {
                breakfast = false;
                lunch = false;
                dinner = false;
            }

            // 添加到退款可用性列表
            refundableDays.add(new RefundAvailabilityVO(date, breakfast, lunch, dinner));
        }

        return AjaxResult.success(refundableDays); // 返回每天的退款可用性列表
    }
}
